package com.ruoyi.business.service.impl;

import cn.hutool.core.comparator.CompareUtil;
import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.business.domain.TResourceInfo;
import com.ruoyi.business.domain.TResourceProgress;
import com.ruoyi.business.mapper.TResourceInfoMapper;
import com.ruoyi.business.mapper.TResourceProgressMapper;
import com.ruoyi.business.service.ITResourceProgressService;
import com.ruoyi.business.vo.TResourceProgressVo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.SecurityUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

/**
 * 学习进度Service业务层处理
 *
 * @author Tellsea
 * @date 2024-07-22
 */
@Service
@RequiredArgsConstructor
public class TResourceProgressServiceImpl extends ServiceImpl<TResourceProgressMapper, TResourceProgress> implements ITResourceProgressService {

    private final TResourceInfoMapper resourceInfoMapper;

    @Override
    public TableDataInfo<TResourceProgressVo> queryList(TResourceProgressVo entity) {
        return PageUtils.buildDataInfo(this.baseMapper.queryList(PageUtils.buildPage(), entity));
    }

    @Override
    public List<TResourceProgressVo> queryAll(TResourceProgressVo entity) {
        return this.baseMapper.queryList(entity);
    }

    @Override
    public TResourceProgressVo queryById(Long id) {
        return this.baseMapper.queryById(id);
    }

    @Override
    public AjaxResult saveEntity(TResourceProgressVo entity) {
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        TResourceProgress resourceProgress = this.baseMapper.selectOne(Wrappers.<TResourceProgress>lambdaQuery()
                .eq(TResourceProgress::getUserId, userId)
                .eq(TResourceProgress::getResourceId, entity.getResourceId()));
        if (Objects.isNull(resourceProgress)) {
            resourceProgress = new TResourceProgress();
            resourceProgress.setUserId(userId);
            resourceProgress.setResourceId(entity.getResourceId());
            resourceProgress.setProgress(entity.getProgress());
            this.baseMapper.insert(resourceProgress);
            return AjaxResult.success("保存成功");
        }
        if (CompareUtil.compare(resourceProgress.getProgress(), entity.getProgress()) < 0) {
            resourceProgress.setProgress(entity.getProgress());
            this.baseMapper.updateById(resourceProgress);
            return AjaxResult.success("保存成功");
        }
        return AjaxResult.success();
    }

    @Override
    public BigDecimal getProgress(Long userId) {
        List<TResourceProgress> resourceProgressList = this.baseMapper.selectList(Wrappers.<TResourceProgress>lambdaQuery()
                .eq(TResourceProgress::getUserId, userId));
        BigDecimal lookTotal = resourceProgressList.stream().map(TResourceProgress::getProgress).reduce(BigDecimal.ZERO, BigDecimal::add);
        int count = resourceInfoMapper.selectCount(Wrappers.<TResourceInfo>lambdaQuery()
                .eq(TResourceInfo::getFileType, 1)
                .eq(TResourceInfo::getIsShow, 1));
        return NumberUtil.mul(NumberUtil.div(lookTotal, BigDecimal.valueOf((count * 100L))), 100).setScale(2, BigDecimal.ROUND_UP);
    }
}
